/*
Copyright (C) 2006 t-arn.com

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
See the GNU General Public License for more details. 
*/

package com.t_arn;

import ewe.fx.*;
import ewe.io.TreeConfigFile;
import ewe.io.TreeConfigNode;
import ewe.ui.*;
import ewe.util.Iterator;
import ewe.util.Vector;
import ewe.sys.Vm;
import ewe.sys.Locale;
import ewe.sys.LocalResource;


//##################################################################
/** This class is used to access LocalResources. Only resource file are supported (not registry)
 * 
 * <pre>Code example:
 * import com.t_arn.taLocalResource;
 * lr = new taLocalResource("MyApp.Main");
 * String textDefault = lr.get("btnSave");
 * lr2 = new taLocalResource("MyApp.Main", "en");
 * String textEnglish = lr2.get("btnSave");
 * 
 * Resource file example:
 * The resource file _config\MyApp.Main.cfg could look like this:
 * {MyApp.Main}
 * {LocalResources}
 * {de}
 * btnSave=Speichern
 * btnEdit=Bearbeiten
 * {..}
 * {en}
 * btnSave=Save
 * btnEdit=Edit
 * {..}
 * {..}
 * {..}
 * </pre>
 * 
 * @since 27.11.06 (version 1.0) - 27.11.06 (version 1.0)
 * @author Tom Arn
 * @author www.t-arn.com
 * @version 1.0
 */
public class taLocalResource
//##################################################################
{
	private static String stVersion = "1.0";
	private LocalResource lr;
	private String modName; // holds the module name
	private static Form fChooser; // language chooser form

//===================================================================
/** Constructor: Creates a local resource for the language shortLanguage.
 * If shortLanguage is null, the systems default language is chosen.
 * If shortLanguage (or the system default language) is not supported by moduleName, the first
 * supported language is chosen.
 * 
 * @param moduleName Name of the resource file
 * @param shortLanguage language, e.g. de, en, fr can be null - then 
 *     language is the system default language
 */
public taLocalResource (String moduleName, String shortLanguage)
//===================================================================
{
	Locale l;
	Vector v;
	
	this.modName = moduleName;
	if ( shortLanguage == null )
	{
		shortLanguage = getDefaultLanguage();
	}  // if
	if ( ! isLanguageAvailable(moduleName, shortLanguage) )
	{
		v = getAvailableLanguages(moduleName);
		if ( v.getCount() >=1 )
		{
			shortLanguage = (String) v.get(0);
		}  // if
		else shortLanguage = "xy";
	}  // if
	l = new Locale(shortLanguage,null, true);		
	this.lr = l.getLocalResource(moduleName,false);
}
//===================================================================
/** Constructor.
 * Gets the language saved with saveCurrentLanguage and
 * calls taLocalResource (String moduleName, String shortLanguage)
 * 
 * @param moduleName Name of the resource file
 */
public taLocalResource (String moduleName)
//===================================================================
{
	this (moduleName, taLocalResource.getSavedLanguage (moduleName));
}
//===================================================================
/** Returns the version of the class.
 * 
 * @return version of this class
 */
public static String getVersion ()
//===================================================================
{
	return stVersion;
}
//===================================================================
/** Presents a modal form that allows to choose a short language from the available
 * languages of the resource file.
 * The form is shown centered on the parentFrame.
 * 
 * @param moduleName Name of the resource file
 * @param parentFrame The parent frame this form should become a child of. 
 *     Can be null.
 * @return returns the chosen short language, e.g. EN
 */
public static String chooseLanguage (String moduleName, Frame parentFrame)
//===================================================================
{
	Dimension dim = new Dimension();
	Vector vlang;
	String[] alang;
	mButton bnOk;
	mChoice mcChooser;
	EventListener elButtons;

	// Event listener for buttons
	elButtons = new EventListener() {
		/** Event handler for language chooser
		 * 
		 * @param ev Event
		 */
		public void onEvent(Event ev){
			if (ev.type == ControlEvent.PRESSED) {
				String stButtonName = ((mButton)ev.target).name;
				if (stButtonName.equals("OK")) 
				{
					fChooser.close(0);
				}
			}
		}
	};

	vlang = getAvailableLanguages(moduleName);
	alang = new String[vlang.getCount()];
	bnOk = new mButton ("OK");
	bnOk.name = "OK";bnOk.addListener(elButtons);
	fChooser = new Form();
	fChooser.setTitle("Choose Language"); 
	for (int i=0 ; i<alang.length ; i++ )
	{
		alang[i] = (String) vlang.get(i);
	}  // for
	mcChooser = new mChoice(alang, 0);
	fChooser.addNext(new mLabel("")).setCell(Form.HSTRETCH);;
	fChooser.addNext(mcChooser).setCell(Form.DONTSTRETCH);
	fChooser.addLast(new mLabel("")).setCell(Form.HSTRETCH);
	fChooser.addNext(new mLabel("")).setCell(Form.HSTRETCH);;
	fChooser.addNext(bnOk).setCell(Form.DONTSTRETCH);
	fChooser.addLast(new mLabel("")).setCell(Form.HSTRETCH);
	fChooser.setFixedSize(140, 50);
	fChooser.execute(parentFrame, Gui.CENTER_FRAME);
	return mcChooser.getText();
}
//===================================================================
/** This finds the local resource (String) as given by key.
 * 
 * @param key Name of the resource
 * @return returns local resource
 */
public String get (String key)
//===================================================================
{
	return (String) this.lr.get(key,key);
}
//===================================================================
/** Retrieves all available languages from the resource file and returns them in a Vector.
 * 
 * @param moduleName Name of the resource file
 * @return returns all available short languages in a Vector of Strings.
 */
public static Vector getAvailableLanguages (String moduleName)
//===================================================================
{
	TreeConfigFile tcf;
	TreeConfigNode tcn;
	Iterator it;
	Vector lang = new Vector();
		
	tcf = TreeConfigFile.getConfigFile("_config/"+moduleName+".cfg");
	if ( tcf == null ) return lang;
	tcn = tcf.find(moduleName+"/LocalResources");
	if (tcn == null) return lang;
	if (tcn.getChildCount() < 1) return lang;

	for (it = tcn.getChildren(); it.hasNext();)
	{
		tcn = (TreeConfigNode) it.next();
		lang.add(tcn.getName());
	}
	return lang;
}
//===================================================================
/** Gets the current language of this instance
 * 
 * @return returns the short language, e.g. EN or "xy" if no language / 
 *     resource file is available
 */
public String getCurrentLanguage ()
//===================================================================
{
	TreeConfigNode tcn;
	
	if ( this.lr instanceof TreeConfigNode)
	{
		tcn = (TreeConfigNode) this.lr;
		return tcn.getName();
	}  // if
	else
		return "xy";
}
//===================================================================
/** Gets the system's default short language
 * 
 * @return returns the short language, e.g. EN
 */
public static String getDefaultLanguage ()
//===================================================================
{
	return new Locale().getString(Locale.LANGUAGE_SHORT,0,0);
}
//===================================================================
/** Gets (from the user registry) the language of the resource file last saved by saveCurrentLanguage
 * 
 * @param moduleName Name of the the resource file
 * @return returns the short language (e.g. EN) or null if no language has 
 *     been saved so far
 */
public static String getSavedLanguage (String moduleName)
//===================================================================
{
	String lang;
	lang = taRegistry.getProgramValue("taLocalResource",moduleName);
	if ( lang.equals("") ) lang = null;
	return lang;
}
//===================================================================
/** Checks if the shortLanguage is available in the resource file.
 * 
 * @param moduleName Name of the the resource file
 * @param shortLanguage language, e.g. de, en, fr
 * @return returns true if language is available, false otherwise
 */
public static boolean isLanguageAvailable (String moduleName, String shortLanguage)
//===================================================================
{
	int i;
	boolean found = false;
	Vector v = getAvailableLanguages (moduleName);
	for ( i=0 ; i<v.getCount(); i++ )
	{
		if ( shortLanguage.equalsIgnoreCase((String)v.get(i)) )
		{
			found = true;
			break;
		}  // if
	}  // for
	return found;
}
//===================================================================
/** Saves the current short language of the resource file to the user registry.
 */
public void saveCurrentLanguage ()
//===================================================================
{
	taRegistry.setProgramValue("taLocalResource",modName,getCurrentLanguage ());
}
//===================================================================
}
//##################################################################

